package com.ruicar.afs.cloud.afscase.autoaudit.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.afscase.autoaudit.condition.ApproveRiskTipsRuleCondition;
import com.ruicar.afs.cloud.afscase.autoaudit.entity.ApproveRiskTipsRule;
import com.ruicar.afs.cloud.afscase.autoaudit.mapper.ApproveRiskTipsRuleMapper;
import com.ruicar.afs.cloud.afscase.autoaudit.service.ApproveRiskTipsRuleService;
import com.ruicar.afs.cloud.bizcommon.rules.service.AfsRuleInfoService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.image.enums.RuleStateEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @author tiankai
 * @Describe 审批风险提示规则接口
 * Created on 2020-05-15
 */
@Slf4j
@RestController
@RequestMapping("/approveRisk")
@AllArgsConstructor
@Api(description = "审批风险提示规则接口")
public class ApproveRiskTipsRuleController {
    private ApproveRiskTipsRuleService service;
    private AfsRuleInfoService afsRuleInfoService;
    private ApproveRiskTipsRuleMapper mapper;

    @RequestMapping(value = "/saveOrUpdateApproveRisk", method = RequestMethod.POST)
    @ApiOperation(value = "更新放款风险提示信息")
    public IResponse<ApproveRiskTipsRule> saveOrUpdateApproveRisk(@RequestBody ApproveRiskTipsRule tipsRule) {
        ApproveRiskTipsRule riskTipsRule = service.getById(tipsRule.getId());
        if (ObjectUtil.isNull(riskTipsRule)) {
            riskTipsRule = new ApproveRiskTipsRule();
            riskTipsRule.setRuleState(RuleStateEnum.EFFECTIVE.getCode());
            riskTipsRule.setRuleName(tipsRule.getRuleName());
            riskTipsRule.setRemarks(tipsRule.getRemarks());
        }
        riskTipsRule.setRuleName(tipsRule.getRuleName());
        riskTipsRule.setRemarks(tipsRule.getRemarks());
        service.saveOrUpdate(riskTipsRule);
        return new IResponse().setData(riskTipsRule);
    }

    @RequestMapping(value = "/updateApproveRiskRule", method = RequestMethod.POST)
    @ApiOperation(value = "修改规则状态")
    public IResponse<ApproveRiskTipsRule> updateApproveRiskRule(@RequestBody ApproveRiskTipsRuleCondition condition) {
        ApproveRiskTipsRule rule = service.getById(condition.getId());
        if (RuleStateEnum.EFFECTIVE.getCode().equals(condition.getRuleState())) {
            afsRuleInfoService.activeRule(condition.getRuleId());
            rule.setRuleState(RuleStateEnum.INVALID.getCode());
        } else if (RuleStateEnum.INVALID.getCode().equals(condition.getRuleState())) {
            afsRuleInfoService.deActiveRule(condition.getRuleId());
            afsRuleInfoService.expiredRule(condition.getRuleId());
            rule.setRuleState(RuleStateEnum.EFFECTIVE.getCode());
        } else {
            rule.setRuleState(RuleStateEnum.EFFECTIVE.getCode());
        }
        service.saveOrUpdate(rule);
        return new IResponse().setData(true);
    }

    @GetMapping(value = "/getApproveRuleInfo")
    @ApiOperation(value = "查询风险提示规则信息")
    public IResponse<IPage<ApproveRiskTipsRuleCondition>> getApproveRuleInfo(@RequestParam(name = "pageNumber") int pageNumber, @RequestParam(name = "pageSize") int pageSize, @RequestParam(required = false) String ruleName,String ruleState) {
        ApproveRiskTipsRuleCondition ruleCondition = new ApproveRiskTipsRuleCondition();
        ruleCondition.setRuleName(ruleName);
        ruleCondition.setRuleState(ruleState);
        IPage<List<ApproveRiskTipsRuleCondition>> list = mapper.getApproveRuleInfo(new Page(pageNumber, pageSize), ruleCondition);
        return IResponse.success(list);
    }
}
